package com.example.administrator.demorecyclerview.refresh;

import android.os.Handler;
import android.os.PersistableBundle;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.OverScroller;
import android.widget.TextView;

import com.example.administrator.demorecyclerview.Adaptern;
import com.example.administrator.demorecyclerview.R;
import com.example.administrator.demorecyclerview.auto_viewpager.HeaderFooterRecyclerView;

import java.lang.invoke.MethodHandle;
import java.util.ArrayList;

public class RefreshActivity extends AppCompatActivity {


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
//        String[] strings = new String[1000000000];
//        outState.putStringArray("1",strings);
    }

    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refresh);
        mViewPager = findViewById(R.id.viewpager);
        final MyListFragment[] fragments = new MyListFragment[1];
        for (int i = 0; i < fragments.length; ++i) {
            fragments[i] = new MyListFragment();
        }
        mViewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), fragments));
        mTabLayout = findViewById(R.id.tablayout);
        mTabLayout.setupWithViewPager(mViewPager);


    }


    private static class ViewPagerAdapter extends FragmentPagerAdapter {

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return String.valueOf(position);
        }

        MyListFragment[] listFragments;

        public ViewPagerAdapter(FragmentManager fm, MyListFragment[] listFragments) {
            super(fm);
            this.listFragments = listFragments;
        }

        @Override
        public Fragment getItem(int position) {
            return listFragments[position];
        }

        @Override
        public int getCount() {
            return listFragments.length;
        }
    }

    public static class MyListFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, MySwipeRefreshLayout.OnLoadListener {
        private HeaderFooterRecyclerView mRecyclerView;
        private MySwipeRefreshLayout mySwipeRefreshLayout;

        Adaptern mAdapter;
        private final Handler mHandler = new Handler();

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_refresh, container, false);
        }


        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            mRecyclerView = view.findViewById(R.id.recyclerview);

            mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            mRecyclerView.setAdapter(mAdapter = new Adaptern(null));

            mRecyclerView.setEmptyView(view.findViewById(R.id.empty));

            mySwipeRefreshLayout = view.findViewById(R.id.swiperefreshlayout);
            mySwipeRefreshLayout.setOnRefreshListener(this);
            mySwipeRefreshLayout.setOnLoadListener(this);
            mySwipeRefreshLayout.autoPostRefresh();
//            onRefresh();

        }

        @Override
        public void onDestroyView() {
            mHandler.removeCallbacksAndMessages(null);
            super.onDestroyView();
        }

        @Override
        public void onRefresh() {
            Log.i("@@", "@@@:onRefresh");

            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    final String[] strings = new String[50];
                    for (int i = 0; i < strings.length; ++i) {
                        strings[i] = String.valueOf(i);
                    }

                    TextView mHeaderView = new TextView(getContext());
                    mHeaderView.setText("Header" + String.valueOf(mRecyclerView.getHeaderCount() + SystemClock.uptimeMillis()));
                    mRecyclerView.addHeaderView(0, mHeaderView);
                    mAdapter.refreshDataArrayWithHasNextPage(null);

                    mySwipeRefreshLayout.setFinished(false);

                }
            }, 1000);
//        Log.i("@@", "@@@:onRefresh");
//
//        final String[] strings = new String[50];
//        for (int i = 0; i < strings.length; ++i) {
//            strings[i] = String.valueOf(i);
//        }
//        mAdapter.refreshDataArrayWithHasNextPage(strings);
//        mySwipeRefreshLayout.setRefreshing(false);
        }

        @Override
        public void onLoading() {
            Log.i("@@", "@@@:onLoading");

            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    final String[] strings = new String[10];
                    for (int i = 0; i < strings.length; ++i) {
                        strings[i] = String.valueOf(mAdapter.getItemCount() + i);
                    }
                    mAdapter.addDataArrayWithHasNextPage(strings);


//                    TextView mFooterView = new TextView(getContext());
//                    mFooterView.setText("Footer" + String.valueOf(mRecyclerView.getFooterCount() + SystemClock.uptimeMillis()));
//                    mRecyclerView.addFooterView(0, mFooterView);
                    mRecyclerView.removeHeaderView(0);
                    mySwipeRefreshLayout.setFinished(false);
                }
            }, 1000);
//        Log.i("@@", "@@@:onLoading");
//
//        final String[] strings = new String[10];
//        for (int i = 0; i < strings.length; ++i) {
//            strings[i] = String.valueOf(mAdapter.getItemCount() + i);
//        }
//        mAdapter.addDataArrayWithHasNextPage(strings);
//        mySwipeRefreshLayout.setLoading(false);


//            scroller.springBack()

        }

    }

//    private final Handler mHandler = new Handler();
//
//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        final OverScroller scroller = new OverScroller(this);
//
//        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
////            scroller.startScroll(0, 0, 10, 0);
////            scroller.springBack(0, 0, 0, 0, 0, 5);
//            scroller.fling(0, 0, 500, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
//            mHandler.post(new Runnable() {
//                @Override
//                public void run() {
//                    if (scroller.computeScrollOffset()) {
//                        Log.i("@@@@@@@@@", "@@@@@@@@@@@:" + scroller.getCurrX()+scroller.springBack(0,0,0,100,0,0));
//                        mHandler.post(this);
//
//                    }
//
//                }
//            });
//        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
////            Log.i("@@@@@@@@@", "@@@@@@@@@@@:" + scroller.springBack(0,0,0,100,0,0));
//
//        }
//        return true;
//    }
}
